package org.jackysoft.cfg.jms;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.servlet.ServletContext;

import org.apache.activemq.command.ActiveMQQueue;
import org.jackysoft.cfg.jms.tomcat.MessageReceiver;
import org.jackysoft.cfg.jms.tomcat.MessageSender;
import org.jackysoft.cfg.jms.tomcat.MessageService;
import org.jackysoft.cfg.jms.tomcat.MyMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

//@org.springframework.context.annotation.Configuration
public class ActivemqConfiguration implements ServletContextAware {

	private ServletContext servletContext;
	private WebApplicationContext wc;   
	private @Value("${brokerURL}") String brokerURL;
    private @Value("${jms.jndiName}") String jndiName;
	private @Value("${jms.destJndiName}") String destJndiName;
    
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
		wc = WebApplicationContextUtils.getWebApplicationContext(this.servletContext);

	}

	public @Bean
	MessageConverter mssageConverter() {
		return new MyMessageConverter();
	}

	public @Bean
	MessageService messageService() {
		return new MessageService();
	}
	
	public @Bean MessageSender messageSender(){
		MessageSender sender = new MessageSender();
		sender.setJmsTemplate(jmsTemplate());
		ActiveMQQueue dest = wc.getBean(ActiveMQQueue.class);
		sender.setDestination(dest);
		return sender;
	}
	
	public @Bean MessageReceiver messageReceiver(){
		MessageReceiver receiver = new MessageReceiver();
		receiver.setConverter((MyMessageConverter) mssageConverter());
		ActiveMQQueue queue = wc.getBean(ActiveMQQueue.class);
		receiver.setDestination(queue);
		return receiver;
		
	}	
	
	public @Bean(name="jmsConnectionFactory") JndiObjectFactoryBean jmsConnectionFactory(){
		JndiObjectFactoryBean  jo = new JndiObjectFactoryBean();
	    jo.setJndiName(jndiName);
		return jo;
	}
	
	public @Bean(name="tomcatQueue") JndiObjectFactoryBean tomcatQueue(){
		JndiObjectFactoryBean  jo = new JndiObjectFactoryBean();
		jo.setJndiName(destJndiName);
		return jo;
	}
	
	public @Bean JmsTemplate jmsTemplate(){
		JmsTemplate jtemp = new JmsTemplate();
		jtemp.setConnectionFactory((ConnectionFactory) jmsConnectionFactory());
		jtemp.setDefaultDestination((Destination) tomcatQueue());
		return jtemp;
	}
	
	public @Bean DefaultMessageListenerContainer listenerContainer(){
		DefaultMessageListenerContainer dl = new DefaultMessageListenerContainer();
		dl.setConnectionFactory( (ConnectionFactory) jmsConnectionFactory());
		dl.setDestination((Destination) tomcatQueue());
		dl.setMessageListener(messageReceiver());
		return dl;
		
	}
	
}
